package com.zhr.liquwang.view.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.liquwang.R;
import com.zhr.liquwang.entity.Shop;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 * Created by 毛懿 on 2016/11/11 11:00.
 * 邮箱：1334964028@qq.com
 */

public class GoodsAdapter extends RecyclerView.Adapter {
    public static final int TYPE_HEAD = -1;
    public static final int TYPE_GOODS = -2;
    private Context context;
    private List<Shop> data;

    public GoodsAdapter(Context context, List<Shop> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_shop_head, parent, false));
            case TYPE_GOODS:
                return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_shop_goods, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            HeadViewHolder holder1 = (HeadViewHolder) holder;
            Glide.with(context).load(data.get(0).getShopFacade()).into(holder1.ivShopHead);
            Glide.with(context).load(data.get(0).getShopLogo()).into(holder1.ivShopLogo);
            holder1.tvHeadTitle.setText(data.get(0).getShopName());
        } else {
            ItemViewHolder holder2 = (ItemViewHolder) holder;
            Log.i("", "onBindViewHolder: ---" + getItemCount());
            Glide.with(context).load(data.get(0).getShopGoodses().get(position - 1).getGoodsMainImage()).into(holder2.ivGoods);
            holder2.tvGoodsTitle.setText(data.get(0).getShopGoodses().get(position - 1).getGoodsTitle());
            holder2.tvGoodsPrice.setText("￥" + data.get(0).getShopGoodses().get(position - 1).getPrice());
            holder2.tvCount.setText("销量:" + data.get(0).getShopGoodses().get(position - 1).getSales() + "件");
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_GOODS;
        }
    }

    @Override
    public int getItemCount() {
        if (data.size() != 0) {
            return data.get(0).getShopGoodses().size() + 1;
        }
        return 0;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        ((GridLayoutManager) recyclerView.getLayoutManager()).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_shop_head)
        ImageView ivShopHead;
        @BindView(R.id.iv_shop_logo)
        ImageView ivShopLogo;
        @BindView(R.id.tv_share)
        TextView tvShare;
        @BindView(R.id.tv_attention)
        TextView tvAttention;
        @BindView(R.id.tv_finds)
        TextView tvFinds;
        @BindView(R.id.tv_head_title)
        TextView tvHeadTitle;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        @BindView(R.id.tv_goods_title)
        TextView tvGoodsTitle;
        @BindView(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @BindView(R.id.tv_count)
        TextView tvCount;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
